package com.fyzk.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fyzk.activity.user.xiangxi.JCOrderDetailActivity;
import com.fyzk.activity.user.xiangxi.NumLotteryOrderDetailActivity;
import com.fyzk.activity.user.xiangxi.OldFootballOrderDetailActivity;
import com.fyzk.bean.BetRecordResponse;
import com.fyzk.betcode.AppLotteryCodeListManager;
import com.fyzk.constants.ConstantsDJ;
import com.fyzk.dialog.SureDialog;
import com.tyjc.lottery.R;
import com.fyzk.net.PublicMethod;
import com.fyzk.utils.IntentParamUtils;

import java.util.List;


public class UserListAdapter extends BaseAdapter {

	private String TAG = this.getClass().getSimpleName();

	private List<BetRecordResponse.OrdersBean> infors;
	protected Context context;

	/**
	 * 跟单提示对话框
	 */
	private SureDialog genDanTiShiDialog = null;
	
	public UserListAdapter(Context context, List<BetRecordResponse.OrdersBean> infors) {
		this.context = context;
		this.infors = infors;
//		genDanTiShiDialog = new SureDialog(this.context, R.style.MyDialog);
	}

	@Override
	public int getCount() {
		if (infors != null) {
			return infors.size();
		} else {
			return 0;
		}
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return infors.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public void notifyDataSetChanged() {
		// TODO Auto-generated method stub
		super.notifyDataSetChanged();
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(
					R.layout.user_explistview, null);
			convertView = LayoutInflater.from(context).inflate(
					R.layout.user_goucai_item, null);
			holder.bg = (LinearLayout) convertView
					.findViewById(R.id.user_exp_child_ll);
			holder.title1 = (TextView) convertView
					.findViewById(R.id.user_exp_title_time);
			holder.title = (TextView) convertView
					.findViewById(R.id.user_exp_child_title);
			holder.wanFa = (TextView) convertView
					.findViewById(R.id.user_exp_child_wanfa);
			holder.time = (TextView) convertView
					.findViewById(R.id.user_exp_child_time);
			holder.state = (TextView) convertView
					.findViewById(R.id.chasebet_all);
			holder.money = (TextView) convertView
					.findViewById(R.id.user_exp_child_money);
			holder.prizeMoney = (TextView) convertView
					.findViewById(R.id.chasebet_beted);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		final BetRecordResponse.OrdersBean info;
		info = infors.get(position);
		holder.title1.setText(info.getReceiveTime() + "");
		holder.title.setText(info.getLotteryTypeName());
		holder.wanFa.setText(ConstantsDJ.wcode(info.getLotteryType()));
		holder.time.setText(info.getReceiveTime() + "");
		if (info.getOrderStatus() == 3) { // 3:已出票
			holder.state.setText(info.getOrderResultStatusName());
		} else {
			holder.state.setText(info.getOrderStatusName());
		}
		holder.money.setText("投注:" + PublicMethod.formatMoney(info.getAmount()) + "米");
		if (info.getTotalPrize() != 0) {
			holder.state.setTextColor(Color.RED);
			holder.prizeMoney.setText("中奖:" + PublicMethod.formatMoney(info.getTotalPrize()*1.0) + "米");
		} else {
			holder.state.setTextColor(Color.BLACK);
			holder.prizeMoney.setText("");
		}

		holder.bg.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (isToGenDan(info.getLotteryType())) {
					String pid = info.getOrderid() + "";
					toOrderDetail(pid, info.getLotteryType()+"");
				}
			}
		});
		return convertView;
	}

	/**
	 * 订单详细页
	 * 
	 * @param id
	 * @param lotteryType
	 */
	protected void toOrderDetail(final String id, String lotteryType) {
		Intent intent;
		if (lotteryType.contains("30") || lotteryType.contains("50")) {
			AppLotteryCodeListManager.betType = AppLotteryCodeListManager.FIRSTENTER;
			intent = new Intent(context, JCOrderDetailActivity.class);
		} else if (lotteryType.contains("40")) {
			intent = new Intent(context, OldFootballOrderDetailActivity.class);
		}else{
			intent = new Intent(context, NumLotteryOrderDetailActivity.class);
			
		}
		intent.putExtra(IntentParamUtils.BETDETAILID, id);
		context.startActivity(intent);
	}
	
	private boolean isToGenDan(String lotteryId){
		int id = 0;
		try {
			id = Integer.parseInt(lotteryId);
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return isToGenDan(id);
	}
	
	private boolean isToGenDan(int lotteryId){
//		switch (lotteryId) {
//		case LotteryIDUtils.DCBF_LOTTERY_ID_INT:
//		case LotteryIDUtils.DCBQC_LOTTERY_ID_INT:
//		case LotteryIDUtils.DCSPF_LOTTERY_ID_INT:
//		case LotteryIDUtils.DCSXDS_LOTTERY_ID_INT:
//		case LotteryIDUtils.DCZJQ_LOTTERY_ID_INT:
//			showTiShiDialog();
//			return false;
//
//		default:
//			break;
//		}
		
		return true;
	}

	public void setInfors(List<BetRecordResponse.OrdersBean> infors) {
		this.infors = infors;
		this.notifyDataSetChanged();
	}


	class ViewHolder {
		LinearLayout bg;
		TextView title1;
		TextView title;
		TextView wanFa;
		TextView time;
		TextView state;
		TextView money;
		TextView prizeMoney;
	}

	/**
	 * 忘记密码对话框
	 */
	private void showTiShiDialog(){
//		genDanTiShiDialog.setTishiString1("");
//		genDanTiShiDialog.setTishiString2("手机端暂未开售此彩种，请到网站进行查看");
//		genDanTiShiDialog.getBtnSure().setValue(Html.fromHtml("<a href=\"http://www.dajiang365.com\">GO>></a> "));
//		genDanTiShiDialog.getBtnSure().setMovementMethod(LinkMovementMethod.getInstance());
//		genDanTiShiDialog.setBtnCancelString("取消");
//		genDanTiShiDialog.setOnDialogOKCancleClickListener(new OnDialogOKCancleClickListener() {
//
//			@Override
//			public void onOKClick() {
//
//				genDanTiShiDialog.dismiss();
//			}
//
//			@Override
//			public void onCancleClick() {
//				// TODO Auto-generated method stub
//				genDanTiShiDialog.dismiss();
//			}
//		});
//		genDanTiShiDialog.show();
	}
}